package com.vpx.server.service.system.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vpx.server.bean.entity.CProjectFlowInstance;
import com.vpx.server.bean.entity.ProjectConfig;
import com.vpx.server.bean.info.system.SysDicTypeInfo;
import com.vpx.server.bean.order.project.CProjectFlowInstanceOrder;
import com.vpx.server.bean.result.system.ProjectConfigResult;
import com.vpx.server.bean.result.system.SysDicTypePageResult;
import com.vpx.server.framework.base.BasePageResult;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.mapper.system.CProjectFlowInstanceMapper;
import com.vpx.server.service.system.CProjectFlowInstanceService;
import com.vpx.server.service.system.SqlExtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Administrator
* @description 针对表【c_project_flow_instance】的数据库操作Service实现
* @createDate 2023-11-18 15:37:22
*/
@Service
public class CProjectFlowInstanceServiceImpl extends ServiceImpl<CProjectFlowInstanceMapper, CProjectFlowInstance>
    implements CProjectFlowInstanceService {
@Autowired
    private CProjectFlowInstanceMapper cProjectFlowInstanceMapper;
    @Resource
    SqlExtService sqlExtService;
    @Override
    public void insert(CProjectFlowInstance projectFlowInstance) {
    cProjectFlowInstanceMapper.add(projectFlowInstance);
    }

    @Override
    public List<CProjectFlowInstance> selectAll() {
        return cProjectFlowInstanceMapper.selectAll();
    }

    @Override
    public void update(CProjectFlowInstance projectFlowInstance) {
    cProjectFlowInstanceMapper.updateById(projectFlowInstance);
    }

    @Override
    public void delete(CProjectFlowInstance projectFlowInstance) {
        cProjectFlowInstanceMapper.deleteById(projectFlowInstance);
    }

    @Override
    public BasePageResult<CProjectFlowInstance> page(CProjectFlowInstanceOrder order) {
        PageHelper.startPage(order.getPageNum(), order.getPageSize());
        QueryWrapper<CProjectFlowInstance> wrapper = new QueryWrapper();
        wrapper.like(StrUtil.isNotBlank(order.getBusType()),"bus_type",order.getBusType());
        wrapper.like(StrUtil.isNotBlank(order.getTypeId()),"type_id",order.getTypeId());
        wrapper.like(StrUtil.isNotBlank(order.getKeyWord()),"instance_name",order.getKeyWord());
        wrapper.orderByDesc("create_time");
        List<CProjectFlowInstance> projects = this.list(wrapper);

        Map<String, JSONObject> map = new HashMap<>();
        if(projects != null && projects.size() > 0) {
            String sql = "select cpfi.id ylid,cywi.id ywid,cywi.name ywname from c_yw cyw,c_project_flow_instance cpfi,c_yw_info cywi where cyw.yl_id = cpfi.id and cyw.yw_id = cywi.id and cyw.yl_id in (";
            for(int i = 0; i < projects.size(); i++) {
                if(i > 0) {
                    sql = sql + ",'";
                } else {
                    sql = sql + "'";
                }
                sql = sql + projects.get(i).getId();
                sql = sql + "'";
            }
            sql = sql + ")";
            List<JSONObject> list = sqlExtService.findListObject(sql, JSONObject.class);

            for(JSONObject obj:list) {
                map.put(obj.getStr("ylid"), obj);
            }
        }

        PageInfo<CProjectFlowInstance> pageInfo = new PageInfo<>(projects);

        PageInfo<CProjectFlowInstance> pageInfo2 = new PageInfo<>();
        BeanUtil.copyProperties(pageInfo,pageInfo2);
        if (CollUtil.isNotEmpty(projects)){
            List<CProjectFlowInstance> resultList = new ArrayList<>();
            projects.forEach(e -> {
                CProjectFlowInstance re = new CProjectFlowInstance();
                BeanUtil.copyProperties(e,re);
                JSONObject obj = map.get(re.getId());
                if(obj != null) {
                    re.setYwTypeId(obj.getStr("ywid"));
                    re.setYwTypeName(obj.getStr("ywname"));
                } else {
                    re.setYwTypeId("");
                    re.setYwTypeName("");
                }
                resultList.add(re);
            });
            pageInfo2.setList(resultList);
        }
        BasePageResult<CProjectFlowInstance> result = new BasePageResult(pageInfo2);
        return result;
    }
}




